package com.example.imgmarker;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.imgmarker.bean.PointBean;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ImageLayout layoutContent;
    private ArrayList<PointBean> pointSimples;
    private String imgUrl;

    Handler handler = new Handler( new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 12:
                    layoutContent.setImgBg( layoutContent.getMeasuredWidth(), layoutContent.getMeasuredHeight(), imgUrl );
                    layoutContent.setPoints( pointSimples );
                    break;
            }
            return false;
        }
    } );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        layoutContent = findViewById( R.id.layoutContent );
        initView();
        initData();
        //延迟加载，获取宽高
        handler.sendEmptyMessageDelayed( 12, 1000 );
    }

    private void initView() {
        layoutContent.setOnClickViewPositionListener( new ImageLayout.OnClickViewPositionListener() {
            @Override
            public void onClickPercent(final double wPercent, final double hPercent) {
                Log.e( "位置：这是外部：", "wPercent:" + wPercent + "hPercent" + hPercent );
                new AlertDialog.Builder( MainActivity.this )
                        .setMessage( "确定添加点？" )
                        .setPositiveButton( "确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                addPoint( wPercent, hPercent );
                            }
                        } )
                        .setNegativeButton( "取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        } ).show();
            }
        } );

        layoutContent.setOnClickMarkerListener( new ImageLayout.OnClickMarkerListener() {
            @Override
            public void onClickPosition(int position) {
                Toast.makeText( MainActivity.this, "index : " + position, Toast.LENGTH_SHORT ).show();
            }
        } );

        layoutContent.setOnChangePositionListener( new ImageLayout.OnChangePositionListener() {
            @Override
            public void onChangePercent(int position, double wPercent, double hPercent) {
                pointSimples.get( position ).setWidthScale( wPercent );
                pointSimples.get( position ).setHeightScale( hPercent );
                layoutContent.setPoints( pointSimples );
            }
        } );
    }

    private void addPoint(double wPercent, double hPercent) {
        PointBean pointSimple = new PointBean( wPercent, hPercent );
        pointSimples.add( pointSimple );
        layoutContent.setPoints( pointSimples );
    }


    private void initData() {

//        imgUrl = "https://img.zcool.cn/community/0136985bc6cd7ba8012099c8a6bd2b.jpg";
        imgUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgm.gmw.cn%2Fattachement%2Fjpg%2Fsite2%2F20161020%2Ff44d305ea09619727dab29.jpg&refer=http%3A%2F%2Fimgm.gmw.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1612596118&t=a65a12ac754e82362cc4a2031df29f33";

        pointSimples = new ArrayList<>();
        PointBean pointSimple100 = new PointBean();
        pointSimple100.widthScale = 0.00f;
        pointSimple100.heightScale = 0.00f;

        PointBean pointSimple1 = new PointBean();
        pointSimple1.widthScale = 0.20f;
        pointSimple1.heightScale = 0.20f;

        PointBean pointSimple2 = new PointBean();
        pointSimple2.widthScale = 0.30f;
        pointSimple2.heightScale = 0.30f;


        PointBean pointSimple3 = new PointBean();
        pointSimple3.widthScale = 0.40f;
        pointSimple3.heightScale = 0.40f;

        PointBean pointSimple4 = new PointBean();
        pointSimple4.widthScale = 0.50f;
        pointSimple4.heightScale = 0.50f;

        PointBean pointSimple5 = new PointBean();
        pointSimple5.widthScale = 0.50f;
        pointSimple5.heightScale = 0.60f;

        PointBean pointSimple6 = new PointBean();
        pointSimple6.widthScale = 1f;
        pointSimple6.heightScale = 1f;

        pointSimples.add( pointSimple100 );
        pointSimples.add( pointSimple1 );
        pointSimples.add( pointSimple2 );
        pointSimples.add( pointSimple3 );
        pointSimples.add( pointSimple4 );
        pointSimples.add( pointSimple5 );
        pointSimples.add( pointSimple6 );

    }
}
